package cn.bjc.org.service.impl;

import cn.bjc.basic.service.impl.BaseServiceImpl;
import cn.bjc.org.domain.Department;
import cn.bjc.org.mapper.DepartmentMapper;
import cn.bjc.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//将当前类自动注入到spring容器中
@Service
//声明将这个类有事务管理
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    
    @Autowired
    private DepartmentMapper departmentMapper;
    
    @Transactional
    @Override
    public void save(Department department) {
        //返回的自增长主键
        departmentMapper.save(department);
        
        //页面传的department ,有没有dirPath的值?
        //情况1:没有上级部门 dirPath = /id
        if(department.getParent_id()==null) {
            Long id = department.getId();
            //设置dirPath
            department.setDirPath("/" + id);
        }else {//情况2:有上级部门
            //通过parent_id获取上级的dirPath
            Long parent_id = department.getParent_id();
            //父级
            Department parentDepartment = departmentMapper.loadById(parent_id);
            //dirPath
            department.setDirPath(parentDepartment.getDirPath()+"/"+department.getId());
        }
        //调用update,更新到数据库
        update(department);
    }

    @Transactional
    @Override
    public void update(Department department) {
        
        //情况1:没有上级部门 dirPath = /id
        if(department.getParent_id()==null) {
            Long id = department.getId();
            //设置dirth
            department.setDirPath("/" + id);
        }else {//情况2:有上级部门
            //通过parent_id获取上级的dirPath
            Long parent_id = department.getParent_id();
            //父级
            Department parentDepartment = departmentMapper.loadById(parent_id);
            //dirPath
            department.setDirPath(parentDepartment.getDirPath()+"/"+department.getId());
        }
        
        departmentMapper.update(department);
    }
    
    @Override
    public List<Department> deptTree() {
        //1.查询所有
        List<Department> all = departmentMapper.loadAll();
        
        //2.准备一个map
        Map<Long,Department> map  = new HashMap<>();
        
        //3.所有数据放入map中
        for (Department department : all) {
            map.put(department.getId(), department);
            
        }
        //4.准备一个集合保存处理之后的数据
        ArrayList<Department> deptTree = new ArrayList<>();
        for (Department department : all) {
            if(department.getParent_id()==null){//顶级部门
                deptTree.add(department);
            }else {//不是顶级部门
                //获取parent_id
                Long parent_id = department.getParent_id();
                //获取上级部门对象
                Department parentDepartment = map.get(parent_id);
                //把自己放入上级部门的children中
                parentDepartment.getChildren().add(department);
            }
        }
        //返回部门树
        return deptTree;
    }
}